﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrmHotel
{
    public partial class FrmMain : Form
    {
        public int ih;
       // public string zong;//消费金额
        public FrmMain()
        {
            InitializeComponent();
        }
        public int AdminId;//登录身份
        private void FrmMain_Load(object sender, EventArgs e)
        {

            if (AdminId==1)
            {
               
                toolStripButton10.Image = imageList1.Images[28];
                toolStripButton9.Image = imageList1.Images[27];
                toolStripButton10.Enabled = false;
                toolStripButton9.Enabled = false;

            }
            else
            {
                toolStripButton10.Image = imageList1.Images[11];
                toolStripButton9.Image = imageList1.Images[26];
            }
            toolStripButton1.Image = imageList1.Images[0];
            toolStripButton2.Image = imageList1.Images[9];
            toolStripButton3.Image = imageList1.Images[7];
            toolStripButton4.Image = imageList1.Images[5];
            toolStripButton5.Image = imageList1.Images[3];
            toolStripButton6.Image = imageList1.Images[14];
            toolStripButton11.Image = imageList1.Images[16];
            toolStripButton12.Image = imageList1.Images[18];
            toolStripButton7.Image = imageList1.Images[24];
            toolStripButton8.Image = imageList1.Images[22];
            
            BoinListView();
            State();

        }
        public void yuojian()
        {
           
        }
        public void State()
        {
            string o = DBHelper.SelectOne(string.Format("select count(1) from roominfo where Rmstate=1")).ToString();
            this.lbl_12.Text = o;
            string o1 = DBHelper.SelectOne(string.Format("select count(1) from roominfo where Rmstate=2")).ToString();
            this.lbl_13.Text = o1;
            string o2 = DBHelper.SelectOne(string.Format("select count(1) from roominfo where Rmstate=3")).ToString();
            this.lbl_14.Text = o2;
            string o3 = DBHelper.SelectOne(string.Format("select count(1) from roominfo where Rmstate=4")).ToString();
            this.lbl_15.Text = o3;
            string sql = DBHelper.SelectOne("select count(1) from roominfo ").ToString();
            this.label7.Text = sql;
        }

        private void toolStripButton1_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton1.Image = imageList1.Images[1];
            toolStripButton1.ForeColor = Color.FromArgb(232, 164, 70);
        }
        private void toolStripButton1_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton1.Image = imageList1.Images[0];
            toolStripButton1.ForeColor = Color.FromArgb(0, 0, 0);
        }
        private void toolStripButton2_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton2.Image = imageList1.Images[8];
            toolStripButton2.ForeColor = Color.FromArgb(232, 164, 70);
        }
        private void toolStripButton2_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton2.Image = imageList1.Images[9];
            toolStripButton2.ForeColor = Color.FromArgb(0, 0, 0);
        }
        private void toolStripButton3_MouseEnter_1(object sender, EventArgs e)
        {
            toolStripButton3.Image = imageList1.Images[6];
            toolStripButton3.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton3_MouseLeave_1(object sender, EventArgs e)
        {
            toolStripButton3.Image = imageList1.Images[7];
            toolStripButton3.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton4_MouseEnter_1(object sender, EventArgs e)
        {
            toolStripButton4.Image = imageList1.Images[4];
            toolStripButton4.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton4_MouseLeave_1(object sender, EventArgs e)
        {
            toolStripButton4.Image = imageList1.Images[5];
            toolStripButton4.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton10_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton10.Image = imageList1.Images[10];
            toolStripButton10.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton10_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton10.Image = imageList1.Images[11];
            toolStripButton10.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton5_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton5.Image = imageList1.Images[2];
            toolStripButton5.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton5_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton5.Image = imageList1.Images[3];
            toolStripButton5.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton6_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton6.Image = imageList1.Images[12];
            toolStripButton6.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton6_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton6.Image = imageList1.Images[14];
            toolStripButton6.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            this.button1.BackColor = Color.FromArgb(187, 227, 229);
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            this.button2.BackColor = Color.FromArgb(187, 227, 229);
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            this.button3.BackColor = Color.FromArgb(88, 149, 153);
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            this.button3.BackColor = Color.FromArgb(187, 227, 229);
        }

        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            this.button4.BackColor = Color.FromArgb(88, 149, 153);
        }

        private void button4_MouseLeave(object sender, EventArgs e)
        {
            this.button4.BackColor = Color.FromArgb(187, 227, 229);
        }   

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
          

        }
        
        public void BoinListView()
        {
       
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=o.RoomId ";
             ListViewItem item=null;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());
                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());

                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                  
                    //this.登记入住ToolStripMenuItem1.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.预定房间ToolStripMenuItem1.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.打扫房间ToolStripMenuItem.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.续住办理ToolStripMenuItem1.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.更换房间ToolStripMenuItem1.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.房间结账ToolStripMenuItem.ForeColor = Color.FromArgb(129, 129, 129);
                    //this.添加消费ToolStripMenuItem1.ForeColor = Color.FromArgb(129, 129, 129);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);

                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);
                    
                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);
                   
                }
                
                listView1.Items.Add(item);
            }
            reader.Close();
            

        }
       
        private void listView1_Click(object sender, EventArgs e)
        {
            Qkong();
        }
        public void Qkong()
        {
        
            this.lbl_1.Text = this.lbl_2.Text = this.lbl_5.Text = this.lbl_3.Text = this.lbl_4.Text = this.lbl_6.Text = this.lbl_7.Text = this.lbl_8.Text = this.lbl_9.Text = this.lbl_10.Text  = string.Empty;
            if (this.listView1.SelectedItems.Count == 0)
            {
                return;
            }

            zong();
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string o = string.Format("select rmstate from roominfo r,Room o where r.RmRoom=o.RoomId and r.RmId=" + id);
           int a=Convert.ToInt32( DBHelper.SelectOne(o));
            string sql = "select r.RmId,o.RoomName,o.RoomMoney  from RoomInfo r,Room o where r.RmRoom=o.RoomId and r.RmId=" + id;
            if (a==3)
            {
                sql = string.Format(@"select r.RmId,o.RoomName,c.CeName,c.CeAge,o.RoomMoney,h.CKCin,h.CKYong,c.CeCut,h.CKXfei,c.CePhone from
                    RoomInfo r,Room o,CetaeInfo c,checkInfo h where r.RmRoom=o.RoomId and c.CeFangHao=r.RmId 
                    and h.Ckname=c.CeId  and r.rmid={0}", id);
            }
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                this.lbl_1.Text = reader["RmId"].ToString();
                this.lbl_2.Text = reader["RoomName"].ToString();
                this.lbl_5.Text = reader["RoomMoney"].ToString() + "￥";
                
                if (a==3)
                {
                    this.lbl_3.Text = reader["CeName"].ToString();
                    this.lbl_4.Text = reader["CeAge"].ToString();
                    this.lbl_6.Text = reader["CKCin"].ToString();
                    this.lbl_7.Text = reader["CeCut"].ToString();
                    this.lbl_8.Text = reader["CKYong"].ToString() + "￥";
                    this.lbl_9.Text = reader["CKXfei"].ToString() + "￥";
                    this.lbl_10.Text = reader["CePhone"].ToString();
                    
                }
            }
            reader.Close();
            XiaoFeiXinfo();//消费信息
           
        }
        List<DateTime> yvlishijian = new List<DateTime>();//接收宾客的预离时间
        List<int> fanghao = new List<int>();
         int id = 0;//接收房间号
        public void ChaoShiTiShi()
        {
          //  ChuLiChaoShi();
            yvlishijian.Clear();
            DateTime dt = DateTime.Now;//当前时间
            string sql = string.Format("select CeCut,CeFangHao from CetaeInfo c,checkInfo o,RoomInfo r where o.Ckname=c.CeId and c.CeFangHao=r.RmId and r.RmState=3 and o.CKState=1");
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                DateTime a = Convert.ToDateTime(reader["CeCut"]);
                fanghao.Add(Convert.ToInt32(reader["CeFangHao"]));
               // id=Convert.ToInt32(reader["CeFangHao"]);
                //a = a.AddHours(-1);
                yvlishijian.Add(a);
            }
            reader.Close();

            for (int i = 0; i < yvlishijian.Count;i++ )
            {
                string b = yvlishijian[i].ToString();
                int fanghao1=fanghao[i];
                DateTime shijian = DateTime.Now;//系统时间
                string a =yvlishijian[i].ToString("yyyy-MM-dd 12:00:00");
                string sql1 = string.Format("select datediff(n,'{0}','{1}')", shijian,b);
                int o = Convert.ToInt32(DBHelper.SelectOne(sql1));
                //if (yvlishijian[i] < shijian && shijian < Convert.ToDateTime(a))
                if (o >0&&o<60)
                {
                    FrmChaoShiTiXing fs = new FrmChaoShiTiXing();
                    //Class1.fanghao = fanghao[i];
                    fs.id = fanghao1;
                      fs.ShowDialog();
                      State();
                      Qkong();
                      BoinListView();
                }
            }
        }
        public void ChuLiChaoShi()
        {
            List<DateTime> yvlishijian = new List<DateTime>();//接收宾客的预离时间
            string sql = string.Format("select CeCut,CeFangHao from CetaeInfo c,checkInfo o,RoomInfo r where o.Ckname=c.CeId and c.CeFangHao=r.RmId and r.RmState=3 and o.CKState=1");
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                DateTime a = Convert.ToDateTime(reader["CeCut"]);
                id=Convert.ToInt32(reader["CeFangHao"]);
            }
            reader.Close();
            DateTime shijian = DateTime.Now;//系统时间

            for (int i = 0; i < yvlishijian.Count; i++)
            {
                string a = yvlishijian[i].ToString("yyy-MM-dd 12:00:00");

                if (shijian > yvlishijian[i])
                {
                    string sql11 = string.Format("update roominfo set rmstate=4 where rmid={0}", id);
                    int ee = DBHelper.ExecuteUpdate(sql11);

                    string sqqq = string.Format("select CeName from CetaeInfo where CeFangHao={0}", id);
                    string name = DBHelper.SelectOne(sqqq).ToString();

                    string sql2 = string.Format("select CeId from CetaeInfo where CeName='{0}'", name);
                    string r = DBHelper.SelectOne(sql2).ToString();

                    string sql1 = string.Format("update  checkinfo set ckstate=3 where Ckname={0}", r);
                    int r1 = DBHelper.ExecuteUpdate(sql1);

                    string qwe = string.Format("delete from Consume  where CsoRoom={0}", id);
                    int r2 = DBHelper.ExecuteUpdate(qwe);

                    if (ee > 0 && r1 > 0)
                    {
                        MessageBox.Show("房间号:" + id + "已超时!自动退房！");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("失败！");
                        return;
                    }
                }
            }
        }
        string r = null;
        public void zong()
        { 
          ListViewItem item = this.listView1.SelectedItems[0];
         int id = Convert.ToInt32(item.Text);
         string sql = string.Format("select sum(CsPrice) from Consume where CsoRoom={0}",id);
          r = DBHelper.SelectOne(sql).ToString();
         if (!string.IsNullOrEmpty(r))
         {
             this.label17.Text = r + "￥";
         }
         else
         {
             this.label17.Text="";
         }
        }
        public void XiaoFeiXinfo()
        {
             ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            dataGridView1.AutoGenerateColumns = false;
            string sql = string.Format("select * from  Consume c,Commodity y,CetaeInfo f where c.SsoName=y.CyId and c.CsoName=f.CeId and c.CsoRoom={0}", id);
            DataTable dt = DBHelper.SelectForTable(sql);
            this.dataGridView1.DataSource = dt;
        }
        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            this.button2.BackColor = Color.FromArgb(88, 149, 153);
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            this.button1.BackColor = Color.FromArgb(88, 149, 153);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=1 and r.RmRoom=o.RoomId ";

            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());

                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());
                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);
                  
                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);
                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);
                }
                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BoinListView();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=2 and r.RmRoom=o.RoomId ";

            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());

                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());
                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);
                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);
                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);
                }
                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=3 and r.RmRoom=o.RoomId ";

            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());

                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());
                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);
                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);
                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);
                }
                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间入住！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                 zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt==1)
            {
                //登记入住
                FrmRuzhu fr = new FrmRuzhu();
                fr.id = id;
                fr.ShowDialog();
                BoinListView();//更新数据
                State();
                Qkong();
            }
            else if (zt==3)
            {
                MessageBox.Show("此房已被入,不能再次入住！", "提示信息！");
                return;
            }
            else if (zt == 2)
            {
                MessageBox.Show("此房已被预定,不能再次入住！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏房,不能登记入住！", "提示信息！");
                return;
            }
        }

        private void toolStripButton11_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton11.Image = imageList1.Images[15];
            toolStripButton11.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton11_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton11.Image = imageList1.Images[16];
            toolStripButton11.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton12_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton12.Image = imageList1.Images[17];
            toolStripButton12.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton12_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton12.Image = imageList1.Images[18];
            toolStripButton12.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton7_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton7.Image = imageList1.Images[23];
            toolStripButton7.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton7_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton7.Image = imageList1.Images[24];
            toolStripButton7.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间预定！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                //预定房间
                FrmYvDing fy = new FrmYvDing();
                fy.id = id;
                ih = id;
                fy.ShowDialog();
                BoinListView();//更新数据
                State();
            }
            else if (zt == 3)
            {
                MessageBox.Show("此房已被入住,不能预定！", "提示信息！");
                return;
            }
            else if (zt == 2)
            {
                MessageBox.Show("此房已被预定,不能再次预定！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏房,不能预定！", "提示信息！");
                return;
            }
        }
      
        private void toolStripButton8_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton8.Image = imageList1.Images[21];
            toolStripButton8.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton8_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton8.Image = imageList1.Images[22];
            toolStripButton8.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间续住！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                MessageBox.Show("此房是空房,不能续住！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
               //续住办理
                FrmXvZhu fx = new FrmXvZhu();
                fx.id = id;
                fx.ShowDialog();
                State();
                Qkong();
                BoinListView();
            }
            else if (zt == 2)
            {
                MessageBox.Show("预约中,不能续住！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏房,不能续住！", "提示信息！");
                return;
            }
        }
        private void toolStripButton12_Click(object sender, EventArgs e)
        {

            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间更换！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                MessageBox.Show("此房空房,不能更换房间！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
                //更换房间
                FrmGengHuan fg = new FrmGengHuan();
                fg.id = id;
                fg.ShowDialog();
                State();
                Qkong();
                BoinListView();
            }
            else if (zt == 2)
            {
                MessageBox.Show("预约中,不能更换房间！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏空房,不能更换房间！", "提示信息！");
                return;
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            FrmSuoDing fs = new FrmSuoDing();
            fs.ShowDialog();
        }

        private void 打扫房间ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间打扫房间！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                MessageBox.Show("此房空房,不能更换房间！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
                MessageBox.Show("宾客在住房,不能打扫房间！", "提示信息！");
                return;
            }
            else if (zt == 2)
            {
                MessageBox.Show("预住期间,不能打扫房间！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                FrmDaSaoFj fd = new FrmDaSaoFj();
                fd.id = id;
                fd.ShowDialog();
                //更新数据
                State();
                Qkong();
                BoinListView();
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                return;
            }
            if (listView1.SelectedItems[0].BackColor == Color.FromArgb(91, 188, 32))
            {
                this.contextMenuStrip1.Items[0].Enabled = true;
                this.contextMenuStrip1.Items[1].Enabled = true;
                this.contextMenuStrip1.Items[2].Enabled = false;
                this.contextMenuStrip1.Items[3].Enabled = false;
                this.contextMenuStrip1.Items[4].Enabled = false;
                this.contextMenuStrip1.Items[5].Enabled = false;
                this.contextMenuStrip1.Items[6].Enabled = false;
                this.contextMenuStrip1.Items[7].Enabled = false;
            }
            else if (listView1.SelectedItems[0].BackColor == Color.FromArgb(232, 164, 70))
            {
                this.contextMenuStrip1.Items[0].Enabled = false;
                this.contextMenuStrip1.Items[1].Enabled = false;
                this.contextMenuStrip1.Items[2].Enabled = false;
                this.contextMenuStrip1.Items[3].Enabled = true;
                this.contextMenuStrip1.Items[4].Enabled = true;
                this.contextMenuStrip1.Items[5].Enabled = true;
                this.contextMenuStrip1.Items[6].Enabled = true;
                this.contextMenuStrip1.Items[7].Enabled = false;
            }
            else if (listView1.SelectedItems[0].BackColor == Color.FromArgb(250, 106, 106))
            {
                this.contextMenuStrip1.Items[0].Enabled = false;
                this.contextMenuStrip1.Items[1].Enabled = false;
                this.contextMenuStrip1.Items[2].Enabled = true;
                this.contextMenuStrip1.Items[3].Enabled = false;
                this.contextMenuStrip1.Items[4].Enabled = false;
                this.contextMenuStrip1.Items[5].Enabled = false;
                this.contextMenuStrip1.Items[6].Enabled = false;
                this.contextMenuStrip1.Items[7].Enabled = false;
            }
            else if (listView1.SelectedItems[0].BackColor == Color.FromArgb(128, 128, 128))
            {
                this.contextMenuStrip1.Items[0].Enabled = false;
                this.contextMenuStrip1.Items[1].Enabled = false;
                this.contextMenuStrip1.Items[2].Enabled = false;
                this.contextMenuStrip1.Items[3].Enabled = false;
                this.contextMenuStrip1.Items[4].Enabled = false;
                this.contextMenuStrip1.Items[5].Enabled = false;
                this.contextMenuStrip1.Items[6].Enabled = false;
                this.contextMenuStrip1.Items[7].Enabled = true;
            }

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间添加消费！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                MessageBox.Show("此房空房,不能添加消费！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
                //添加消费
                FrmTianJiaXF xf = new FrmTianJiaXF();
                xf.id = id;
                xf.ShowDialog();
            }
            else if (zt == 2)
            {
                MessageBox.Show("预住期间,不能添加消费！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
               MessageBox.Show("此房是脏空房,不能添加消费！", "提示信息！");
                return;
            }
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
           
            YvDing();          
            State();
            Qkong();
            BoinListView();
          
            

        }
        List<DateTime> shijian = new List<DateTime>();
        List<string> Fangming = new List<string>();
        public void YvDing()
        {
            shijian.Clear();
            Fangming.Clear();
            string sql = "select * from ResvInfo where RvSatse=2";
            SqlDataReader read = DBHelper.SelectMore(sql);
            while (read.Read())
            {
                shijian.Add(Convert.ToDateTime(read["RvCut"]));
                Fangming.Add(read["RvFangHao"].ToString());
            }
            read.Close();
            for (int i = 0; i < Fangming.Count; i++)
            {
                DateTime dangqianshijian = DateTime.Now;

                string shicha = string.Format("select datediff(n,'{0}','{1}')", shijian[i], dangqianshijian);
                int cha = Convert.ToInt32(DBHelper.SelectOne(shicha));
                if (cha <= 120 && cha >= 0)
                {
                    string sql1 = String.Format("update RoomInfo set RmState=2  where  RmId='{0}' and RmState=1", Fangming[i]);
                    int hangshu = DBHelper.ExecuteUpdate(sql1);
                    if (hangshu > 0)
                    {
                        BoinListView();
                        State();
                        Qkong();
                    }
                }
                else if (cha > 120 || cha < 0)
                {
                    string sql1 = string.Format("update RoomInfo set RmState=1  where  RmId='{0}' and RmState=2 ", Fangming[i]);
                    int hang = DBHelper.ExecuteUpdate(sql1);
                    if (hang > 0)
                    {
                        string sql2 = string.Format("update ResvInfo set RvSatse=3 where  RvFangHao='{0}' ", Fangming[i]);
                        int result = DBHelper.ExecuteUpdate(sql2);
                    }
                }

            }

        }

        private void 预客入住ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间预定！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select RmState from RoomInfo where RmId=" + id;
            int zt=Convert.ToInt32(DBHelper.SelectOne(sql));

            if (zt == 1)
            {
                MessageBox.Show("此房空房,不能预客入住！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
                MessageBox.Show("再住期间,不能预客入住！", "提示信息！");
                return;
            }
            else if (zt == 2)
            {
                FrmYvKeRuZhu fyk = new FrmYvKeRuZhu();
                fyk.id = id;
                fyk.ShowDialog();
                State();
                Qkong();
                BoinListView();
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏房,不能预客入住！", "提示信息！");
                return;
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            Qkong();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请至少选择一间房间离店结账！", "提示信息！");
                return;
            }
            ListViewItem item = this.listView1.SelectedItems[0];
            int id = Convert.ToInt32(item.Text);
            string sql = "select * from RoomInfo where RmId=" + id;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            int zt = -1;
            if (reader.Read())
            {
                zt = Convert.ToInt32(reader["RmState"]);
            }
            reader.Close();
            if (zt == 1)
            {
                MessageBox.Show("此房空房,不能结账！", "提示信息！");
                return;
            }
            else if (zt == 3)
            {
                FrmJieZhang fj = new FrmJieZhang();
                fj.id = id;
                fj.Money = r;
                fj.ShowDialog();
                string sql11 = "delete from  ZhiFuMx  where Zmd=1";
                int s = DBHelper.ExecuteUpdate(sql11);
                string sql1 = "delete from ZhiFuMx  where Zmd=2";
                int s1 = DBHelper.ExecuteUpdate(sql1);
                string sql2 = "delete from ZhiFuMx  where Zmd=3";
                int s2 = DBHelper.ExecuteUpdate(sql2);
                string sql3 = "delete from ZhiFuMx  where Zmd=4";
                int s3 = DBHelper.ExecuteUpdate(sql3);
                //更新数据
                State();
                Qkong();
                BoinListView();

            }
            else if (zt == 2)
            {
                MessageBox.Show("预住期间,不能结账！", "提示信息！");
                return;
            }
            else if (zt == 4)
            {
                MessageBox.Show("此房是脏空房,不能结账！", "提示信息！");
                return;
            }
        }

        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.LargeIcon;
        }

        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.List;
        }

        private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Tile;
        }

        private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            this.listView1.View = View.Details;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.SmallIcon;
        }

        private void 空房ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=o.RoomId and r.RmState=1";
            ListViewItem item = null;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());
                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());

                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);

                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);

                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);

                }

                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void 预住ToolStripMenuItem_Click(object sender, EventArgs e)
        {
        this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=o.RoomId and r.RmState=2";
            ListViewItem item = null;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());
                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());

                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);

                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);

                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);

                }

                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void 再住ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=o.RoomId and r.RmState=3";
            ListViewItem item = null;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());
                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());

                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);

                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);

                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);

                }

                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void 脏房ToolStripMenuItem_Click(object sender, EventArgs e)
        {
        this.listView1.Items.Clear();
            string sql = "select r.RmId,o.RoomName,o.RoomMoney,s.StName from RoomInfo r,Room o,RmState s  where s.StId=r.RmState and r.RmRoom=o.RoomId and r.RmState=4";
            ListViewItem item = null;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            while (reader.Read())
            {
                item = new ListViewItem(reader["RmId"].ToString());
                //item.BackColor = Color.Red;
                item.SubItems.Add(reader["RoomName"].ToString());
                item.SubItems.Add(reader["RoomMoney"].ToString());
                item.SubItems.Add(reader["StName"].ToString());

                if (reader["StName"].Equals("空闲"))
                {
                    item.BackColor = Color.FromArgb(91, 188, 32);
                }
                else if (reader["StName"].Equals("预住"))
                {
                    item.BackColor = Color.FromArgb(250, 106, 106);

                }
                else if (reader["StName"].Equals("已住"))
                {
                    item.BackColor = Color.FromArgb(232, 164, 70);

                }
                else if (reader["StName"].Equals("脏房"))
                {
                    item.BackColor = Color.FromArgb(128, 128, 128);

                }

                listView1.Items.Add(item);
            }
            reader.Close();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Hide();
            FrmDenglu fd = new FrmDenglu();
            fd.ShowDialog();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            FrmShangPinGL fs = new FrmShangPinGL();
            fs.ShowDialog();
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
              DialogResult dr = MessageBox.Show("确定要退出吗？","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
              if (dr == DialogResult.Yes)
              {
                  this.Close();
              }
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            Project.Form1 f = new Project.Form1();
            f.ShowDialog();
            //更新数据
            State();
            Qkong();
            BoinListView();
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            xiangmu1.HY_GL gl = new xiangmu1.HY_GL();
            gl.ShowDialog();
            State();
            Qkong();
            BoinListView();
        }

        private void toolStripButton9_MouseEnter(object sender, EventArgs e)
        {
            toolStripButton9.Image = imageList1.Images[25];
            toolStripButton9.ForeColor = Color.FromArgb(232, 164, 70);
        }

        private void toolStripButton9_MouseLeave(object sender, EventArgs e)
        {
            toolStripButton9.Image = imageList1.Images[26];
            toolStripButton9.ForeColor = Color.FromArgb(0, 0, 0);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            YvDing();
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            FrmTongJi f = new FrmTongJi();
            f.ShowDialog();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ChaoShiTiShi();
        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            ChaoShiTiShi();
        }

        private void toolStripMenuItem2_Click_1(object sender, EventArgs e)
        {
            ChaoShiTiShi();
        }
        
    }
}
